qui(
    function (require, exports, module) {
        //素材管理
        WrapSys.MaterialManager = (function () {
            var entityIdIndex = 0;
            var timerId = -1;
            var lastQueryName1 = null;
            var source = null;
            var _openMode = null;

            function initMaterialSelDlg() {
                $("#material_sel_dlg").dialog({
                    title: "选择素材",
                    resizable: false,
                    dialogClass: 'alert',
                    autoOpen: false,
                    width: 720,
                    modal: true,
                    close: function () {
                        console.log("#material_sel_dlg close");
                        if (timerId != -1) {
                            clearInterval(timerId);
                        }
                        $("#material_sel_content").html("");
                    }
                })
                var selElement = $("#material_sel_dlg").find("select[name=EQI_contentType]");
                var htmlArr = [];
                htmlArr.push('<option value="">全部类型</option>');
                for (var tempKey in WrapSys.Config.MATERIAL_CONTENT_TYPE) {
                    htmlArr.push('<option value="' + tempKey.replace(/key/, "") + '">' + WrapSys.Config.MATERIAL_CONTENT_TYPE[tempKey] + '</option>');
                }
                selElement.html(htmlArr.join());
                $("#material_sel_content").delegate("div[selRow]", "click", function (e) {
                    console.log($(e.currentTarget).attr("selRow"));

                    var findTarget = null;
                    var targetId = parseInt($(e.currentTarget).attr("selRow"));
                    $.each(source, function (i, element) {
                        if (element.id == targetId) {
                            findTarget = element;
                            return false;
                        }
                    })
                    if (findTarget != null) {
                        if (_openMode == "video") {
                            WrapSys.ShimManager.addEntity(findTarget);
                        } else if (_openMode == "image") {
                            WrapSys.CornerManager.addCornPic(findTarget)
                        }
                    }
                    $("#material_sel_dlg").dialog("close");
                });

                $("select[name=EQI_contentType]").bind("change",function(){
                    $("#material_sel_content").html("");
                    lastQueryName1 = null;
                });
            }

            function fillMaterialSelDlg(data) {
                source = data;
                var $content = $("#material_sel_content");
                $.each(data, function (idx, element) {
                    makeUpSelDiv(element).appendTo($content);
                    $("<br>").appendTo($content);
                })
            }

            function fillMaterialNoResult() {
                source = [];
                var $content = $("#material_sel_content");
                $content.html("对不起，没有这个结果!")
            }

            function makeUpSelDiv(element) {
                var htmlArr = [];
                htmlArr.push('<div selRow="' + element.id + '">');
                htmlArr.push("<span style='display: inline-block;'><img src='" + element.url + "' /></span>");
                htmlArr.push("<span style='display: inline-block;margin-left: 15px;width: 200px;word-break:break-all ;'>" + element.name + "</span>");
                htmlArr.push("<span style='display: inline-block;width: 60px'>" + element.type + "</span>");
                htmlArr.push("<span style='display: inline-block;width: 120px'>" + element.type == "图片" ? "" : element.contentType + "</span>");
//                htmlArr.push("<span style='display: inline-block;width: 60px'>" + element.time + "</span>");
                htmlArr.push("</div>")
                return $(htmlArr.join(""));
            }

            function openMaterialSelDlg(openMode) {
                _openMode = openMode;
                if (_openMode == "image") {
                    var selectRow = WrapSys.CornerManager.getSelectEntity();
                    if (selectRow == null) {
                        WrapSys.Plugin.tips("error", WrapSys.Config.REMIND_MSG_INFO.NOT_BIND_CORNER);
                        return false;
                    }
                    if(selectRow.sTime == -1){
                        WrapSys.Plugin.tips("error", WrapSys.Config.REMIND_MSG_INFO.BIND_CORNER_START_FIRST);
                        return false;
                    }
                }
                $("#material_sel_dlg").dialog({title: "选择素材" + (_openMode == "image" ? "图片" : "(视频)")});
                $("#material_sel_dlg").dialog("open");
                $("input[name=input_suggest_material]").val("");
                $("select[name=EQI_contentType]").val("");
                $("select[name=EQI_contentType]").hide();
                if (_openMode == "video") {
                    $("select[name=EQI_contentType]").show();
                }
                lastQueryName1 = null;
                $("#material_sel_content").html("");
                timerId = setInterval(function () {
                    var sendData = {  LIKES_name: ""};
                    if (_openMode == "video") {
                        sendData["EQI_type"] = 0;
                    } else if (_openMode == "image") {
                        sendData["EQI_type"] = 1;
                    }
                    var currentQuery = $("input[name=input_suggest_material]").val();
                    if (currentQuery.length == 0 || lastQueryName1 == currentQuery) {
                        return;
                    }
                    lastQueryName1 = currentQuery;
                    $("#material_sel_content").html("");
                    sendData["LIKES_name"] = lastQueryName1;
                    if ($("select[name=EQI_contentType]").val().length > 0) {
                        sendData["EQI_contentType"] = $("select[name=EQI_contentType]").val();
                    }
                    WrapSys.Dao.getMaterialList(sendData, function (rdata) {
                        if(sendData["EQI_contentType"]!=null && sendData["EQI_contentType"]!= $("select[name=EQI_contentType]").val()){
                            console.log(" interrupt callback ,wait for next");
                            return;
                        }
                        if (rdata != null && rdata.code == WrapSys.Config.CODE_SUC) {
                            if (rdata.data.result.length > 0) {
                                fillMaterialSelDlg(rdata.data.result);
                            } else {
                                fillMaterialNoResult();
                            }
                            WrapSys.Plugin.tips("success", "请求成功")
                        } else {
                            WrapSys.Plugin.tips("error", "请求失败")
                        }
                    })
                }, 1500)
            }

            function isDlgOpen() {
                return  $("#material_sel_dlg").is(":visible");
            }

            //  function remove
            return {
                initMaterialSelDlg: initMaterialSelDlg,
                openMaterialSelDlg: openMaterialSelDlg,
                isDlgOpen: isDlgOpen
            }
        })();
    }
);